<?php 

namespace IUTINFO\ActionsComBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpFoundation\Session\Session;

class ActionsComController extends Controller
{
	public function accueilAction()
	{
		$onglet_ouvert = "accueil";
		
		return $this->render('IUTINFOActionsComBundle:ActionsCom:accueil.html.twig', array(
			'onglet_ouvert'	=> $onglet_ouvert,
		));
	}
	
	public function voirActionsAction()
	{	
		$onglet_ouvert = "actiondecommunication";
		
		$repository = $this	->getDoctrine()
							->getManager('db_adc')
							->getRepository('IUTINFOActionsComBundle:ActionDeCommunication');
		$actions = $repository->findAll();
		
		return $this->render('IUTINFOActionsComBundle:ActionsCom:voir_actions.html.twig', array(
			'onglet_ouvert' => $onglet_ouvert,
			'actions' => $actions,
		));
	}
	
	public function detailActionAction($id)
	{
		if($this->container->get('request')->getSession()->get('utilisateur_role') == null)
		{
			$onglet_ouvert = "accueil";
			$error = "Vous devez être authentifié.";
				
			return $this->render('IUTINFOActionsComBundle:ActionsCom:accueil.html.twig', array(
					'onglet_ouvert'	=> $onglet_ouvert,
					'error'			=> $error,
			));
		}
		
		$onglet_ouvert = "actiondecommunication";
		
		if($id < 1)
			throw $this->createNotFoundException('Action inexistante.');
		
		$repository = $this	->getDoctrine()
							->getManager('db_adc')
							->getRepository('IUTINFOActionsComBundle:ActiondeCommunication');
		$action = $repository->find($id);
		
		return $this->render('IUTINFOActionsComBundle:ActionsCom:detail_action.html.twig', array(
			'onglet_ouvert' => $onglet_ouvert,
			'action' => $action,
		));
	}
	
	public function voirForumsAction($id)
	{
		$onglet_ouvert = "actiondecommunication";
		
		if($id < 1)
			throw $this->createNotFoundException('Forum inexistant.');
	
		$repository = $this	->getDoctrine()
							->getManager('db_adc');
		
		$action = $repository	->getRepository('IUTINFOActionsComBundle:ActionDeCommunication')
								->find($id);
		
		$forums = $repository	->getRepository('IUTINFOActionsComBundle:Forum')
								->findByAction($id);
	
		return $this->render('IUTINFOActionsComBundle:ActionsCom:accueil.html.twig', array(
				'onglet_ouvert' => 	$onglet_ouvert,
				'action' 		=>	$action,
				'forums' 		=> 	$forums,
		));
	}
	
	public function detailForumAction($id)
	{
		if($this->container->get('request')->getSession()->get('utilisateur_role') == null)
		{
			$onglet_ouvert = "accueil";
			$error = "Vous devez être authentifié.";
		
			return $this->render('IUTINFOActionsComBundle:ActionsCom:voir_actions.html.twig', array(
					'onglet_ouvert'	=> $onglet_ouvert,
					'error'			=> $error,
			));
		}
		
		$onglet_ouvert = "actiondecommunication";
	
		if($id < 1)
			throw $this->createNotFoundException('Forum inexistante.');
	
		$repository = $this	->getDoctrine()
							->getManager('db_adc')
							->getRepository('IUTINFOActionsComBundle:Forum');
		$forum = $repository->find($id);
	
		return $this->render('IUTINFOActionsComBundle:ActionsCom:detail_forum.html.twig', array(
				'onglet_ouvert' => $onglet_ouvert,
				'forum' => $forum,
		));
	}
	
	public function mesActionsAction()
	{
		$repository = $this	->getDoctrine()
							->getManager('db_adc');
		
		switch($this->container->get('request')->getSession()->get('utilisateur_role'))
		{
			case "administrateur":
			case 'enseignant':
				$onglet_ouvert = "actiondecommunication";
				
				$actions = $repository	->getRepository('IUTINFOActionsComBundle:ActionDeCommunication')
										->findByEnseignant($this->container->get('request')->getSession()->get('utilisateur')[0]->getId());
				
				$forums = $repository	->getRepository('IUTINFOActionsComBundle:Forum')
										->findByEnseignant($this->container->get('request')->getSession()->get('utilisateur')[0]->getId());
				
				return $this->render('IUTINFOActionsComBundle:ActionsCom:mes_actions.html.twig', array(
						'onglet_ouvert'	=> $onglet_ouvert,
						'actions'		=> $actions,
						'forums'		=> $forums,
				));
			case 'etudiant':
				$onglet_ouvert = "actiondecommunication";
				
				$actions = $repository	->getRepository('IUTINFOActionsComBundle:ActionDeCommunication')
										->findByEnseignant($this->container->get('request')->getSession()->get('utilisateur')[0]->getId());
				
				$forums = $repository	->getRepository('IUTINFOActionsComBundle:Forum')
										->findByEnseignant($this->container->get('request')->getSession()->get('utilisateur')[0]->getId());
				
				return $this->render('IUTINFOActionsComBundle:ActionsCom:mes_actions.html.twig', array(
						'onglet_ouvert'	=> $onglet_ouvert,
						'actions'		=> $actions,
						'forums'		=> $forums,
				));
			default:
				$onglet_ouvert = "accueil";
				$error = "Vous devez être authentifié.";
	
				return $this->render('IUTINFOActionsComBundle:ActionsCom:accueil.html.twig', array(
						'onglet_ouvert'	=> $onglet_ouvert,
						'error'			=> $error,
				));
		}
	}
}